package com.k3cloud.conntroller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.AES;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.k3cloud.user.service.ErpBomService;
import com.ty.basic.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/rest/v1/erpBom")
public class ErpBomController {

    @Autowired
    ErpBomService erpBomService;

    public ErpBomController() {
    }


    @PostMapping("/list")
    public ResponseResult<PageInfo<Map<String,Object>>> list(@RequestBody Map<String,Object> map) {
        PageMethod.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"), true);
        List<Map<String,Object>> list = erpBomService.getErpBom(map);
        PageInfo<Map<String,Object>> result = new PageInfo(list);
        return ResponseResult.success(result);
    }

    @PostMapping("/selectFiguration")
    public ResponseResult<PageInfo<Map<String,Object>>> selectFiguration(@RequestBody Map<String,Object> map) {
        PageMethod.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"), true);
        List<Map<String,Object>> list = erpBomService.selectFiguration(map);
        PageInfo<Map<String,Object>> result = new PageInfo(list);
        return ResponseResult.success(result);
    }

    @RequestMapping("/updateFiguration")
    public ResponseResult updateFiguration(@RequestBody Map<String,Object> jsonMap) {
        Map<String,Object> map=JSONObject.parseObject(String.valueOf(jsonMap.get("json"))).getInnerMap();
        String password= (String) map.get("password");
        if(password!=null){
            String aesPassword= AES.encrypt(password,"huazPlm@15999899");
            map.put("password",aesPassword);
        }
        Map<String,String> save=new HashMap<>();
        for(Map.Entry<String,Object> entity: map.entrySet()){
            if(entity.getValue() instanceof String){
                save.put(entity.getKey(), (String) entity.getValue());
            }
            else if(entity.getValue() instanceof Integer){
                save.put(entity.getKey(), ((Integer) entity.getValue()).toString());
            }
            else if(entity.getValue() instanceof Long){
                save.put(entity.getKey(), ((Long) entity.getValue()).toString());
            }
            else{
                save.put(entity.getKey(), (String) entity.getValue());
            }
        }

        erpBomService.updateFiguration(save);
        return ResponseResult.success();
    }
    @RequestMapping("/saveFiguration")
    public ResponseResult saveFiguration(@RequestBody Map<String,String> jsonMap) {
        String password= (String) jsonMap.get("password");
        String aesPassword= AES.encrypt(password,"huazPlm@15999899");
        jsonMap.put("password",aesPassword);
        jsonMap.put("id",UUID.randomUUID().toString());
        erpBomService.saveFiguration(jsonMap);
        return ResponseResult.success();
    }
    @PostMapping("/getConfigurationType")
    public ResponseResult<PageInfo<Map<String,Object>>> getConfigurationType(@RequestBody Map<String,Object> map) {
        List<Map<String,Object>> list = erpBomService.getConfigurationType(map);
        return ResponseResult.success(list);
    }
}
